package com.lee.boot.head.first.demo.redis.controller;

import com.lee.boot.head.first.demo.redis.entity.Owner;
import com.lee.boot.head.first.demo.redis.service.OwnerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/owner")
public class OwnerController {

    @Autowired
    private OwnerService ownerService;

    @RequestMapping(value = "")
    public Owner insertOwner(Owner owner) {
        LocalDateTime now = LocalDateTime.now();

        Date nowDate = Date.from(now.atZone(ZoneOffset.systemDefault()).toInstant());
        owner.setCreateTime(nowDate);
        return ownerService.insertOwner(owner);
    }

    @RequestMapping(value = "get")
    public Owner getOneByName(String name) {
        return ownerService.getOneByName(name);
    }

    @RequestMapping(value = "list")
    public List<Owner> listOwner() {
        return ownerService.listOwner();
    }

    @RequestMapping(value = "{id}")
    public Owner getOwner(@PathVariable Long id) {
        return ownerService.getOwner(id);
    }

    @RequestMapping(value = "delete/{id}")
    public void deleteOwner(@PathVariable Long id) {
        ownerService.deleteOwner(id);
    }

}
